package cn.iocoder.yudao.module.jikong.controller.admin.jkOrder;

import cn.hutool.json.JSONObject;
import cn.iocoder.yudao.framework.security.core.service.SecurityFrameworkService;
import cn.iocoder.yudao.module.jikong.controller.admin.address.vo.AddressRespVO;
import cn.iocoder.yudao.module.jikong.controller.admin.jkProduct.vo.JkProductRespVO;
import cn.iocoder.yudao.module.jikong.dal.dataobject.address.AddressDO;
import cn.iocoder.yudao.module.jikong.dal.dataobject.jkProduct.JkProductDO;
import cn.iocoder.yudao.module.jikong.service.address.AddressService;
import cn.iocoder.yudao.module.jikong.service.jkProduct.JkProductService;
import cn.iocoder.yudao.module.jikong.service.jkProductType.JkProductTypeService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.jikong.controller.admin.jkOrder.vo.*;
import cn.iocoder.yudao.module.jikong.dal.dataobject.jkOrder.JkOrderDO;
import cn.iocoder.yudao.module.jikong.service.jkOrder.JkOrderService;

@Tag(name = "管理后台 - 单据")
@RestController
@RequestMapping("/jikong/jk-order")
@Validated
public class JkOrderController {

    @Resource
    private JkOrderService jkOrderService;
    @Resource
    private JkProductService jkProductService;
    @Resource
    private JkProductTypeService jkProductTypeService;
    @Resource
    private AddressService addressService;
    @Resource
    private SecurityFrameworkService securityFrameworkService;

    @PostMapping("/create")
    @Operation(summary = "创建单据")
    @PreAuthorize("@ss.hasPermission('jikong:jk-order:create')")
    public CommonResult<Long> createJkOrder(@Valid @RequestBody JkOrderSaveReqVO createReqVO) {
        return success(jkOrderService.createJkOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新单据")
    @PreAuthorize("@ss.hasPermission('jikong:jk-order:update')")
    public CommonResult<Boolean> updateJkOrder(@Valid @RequestBody JkOrderSaveReqVO updateReqVO) {
        jkOrderService.updateJkOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除单据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('jikong:jk-order:delete')")
    public CommonResult<Boolean> deleteJkOrder(@RequestParam("id") Long id) {
        jkOrderService.deleteJkOrder(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除单据")
                @PreAuthorize("@ss.hasPermission('jikong:jk-order:delete')")
    public CommonResult<Boolean> deleteJkOrderList(@RequestParam("ids") List<Long> ids) {
        jkOrderService.deleteJkOrderListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得单据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('jikong:jk-order:query')")
    public CommonResult<JkOrderRespVO> getJkOrder(@RequestParam("id") Long id) {
        JkOrderDO jkOrder = jkOrderService.getJkOrder(id);

        //根据单据对应的产品，查询产品相关信息
        JkProductDO jkProduct = jkProductService.getJkProduct(jkOrder.getProductId());
        JkProductRespVO bean = BeanUtils.toBean(jkProduct, JkProductRespVO.class);
        bean.setTypeName(jkProductTypeService.getJkProductType(bean.getTypeId()).getName());
        JkOrderRespVO result = BeanUtils.toBean(jkOrder, JkOrderRespVO.class);
        result.setProductData(bean);
        AddressDO address = addressService.getAddress(1L);
        AddressRespVO addressData = BeanUtils.toBean(address, AddressRespVO.class);
        result.setAddressData(addressData);
        return success(result);
    }

    @GetMapping("/page")
    @Operation(summary = "获得单据分页")
    @PreAuthorize("@ss.hasPermission('jikong:jk-order:query')")
    public CommonResult<PageResult<JkOrderRespVO>> getJkOrderPage(@Valid JkOrderPageReqVO pageReqVO) {

        //判断是否有某个角色
        boolean b = securityFrameworkService.hasRole("order_expert");
        //如果是专家，则查询状态非1的所有状态
        if (b) {
            PageResult<JkOrderDO> pageResult = jkOrderService.getJkOrderPageExpert(pageReqVO);
            return success(BeanUtils.toBean(pageResult, JkOrderRespVO.class));
        }
        PageResult<JkOrderDO> pageResult = jkOrderService.getJkOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JkOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出单据 Excel")
    @PreAuthorize("@ss.hasPermission('jikong:jk-order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportJkOrderExcel(@Valid JkOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<JkOrderDO> list = jkOrderService.getJkOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "单据.xls", "数据", JkOrderRespVO.class,
                        BeanUtils.toBean(list, JkOrderRespVO.class));
    }


    @PutMapping("/updateStatus")
    @Operation(summary = "更新状态")
    @PreAuthorize("@ss.hasPermission('jikong:jk-order:update')")
    public CommonResult<Boolean> updateJkOrderStatus(@Valid @RequestBody JkOrderStatusReqVO updateReqVO) {
        JkOrderSaveReqVO updateStatusReqVO = new JkOrderSaveReqVO();
        BeanUtils.copyProperties(updateReqVO, updateStatusReqVO);
        jkOrderService.updateJkOrder(updateStatusReqVO);
        return success(true);
    }


    @GetMapping("/UserOrderPage")
    @Operation(summary = "获得单据分页-用于机构和用户的单据查询  只用传用户id就行")
    public CommonResult<PageResult<JkOrderRespVO>> getJkOrderPageApp(@Valid JkOrderPageReqVO pageReqVO) {
        PageResult<JkOrderDO> pageResult = jkOrderService.getJkOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JkOrderRespVO.class));
    }

    @GetMapping("/statusCount")
    @Operation(summary = "查询所有类型的数量")
    public CommonResult<List<JSONObject>> statusCount() {
        List<JSONObject> countMap = jkOrderService.statusCount();
        return success(countMap);
    }

}
